﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;

using System.Collections.Generic;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace WebApplication1
{
    public class CalendarData
    {
        EventFeed eventFeed;
        public Dictionary<string, EventEntry> events = new Dictionary<string, EventEntry>();
        private CalendarService myService = new CalendarService("GoSlow");
        private string user;
        private string pass;

        public CalendarData (DateTime time, string userName, string password)
        {
            // Create a CalenderService and authenticate
            //CalendarService myService = new CalendarService("GoSlow");
            user = userName;
            pass = password;

            myService.setUserCredentials(userName, password);

            CalendarQuery query = new CalendarQuery();
            query.Uri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
            CalendarFeed resultFeed = (CalendarFeed)myService.Query(query);
            Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry();
            
            EventQuery eventQuery = new EventQuery("http://www.google.com/calendar/feeds/default/private/full");
            eventQuery.StartTime = new DateTime(time.Year, time.Month, 1).AddDays(-7);
            eventQuery.EndTime = new DateTime(time.Year, time.Month + 1, 8);

            eventFeed = myService.Query(eventQuery);

            int index = 0;
            foreach (Google.GData.Calendar.EventEntry myEntry in eventFeed.Entries)
            {
                When w = myEntry.Times[0];
                if(!events.ContainsKey(w.StartTime.ToShortDateString() + ":" + w.StartTime.Hour))
                events.Add(w.StartTime.ToShortDateString() + ":" + w.StartTime.Hour, myEntry);
            }
        }

        public string timeToString(DateTime dt)
        {
            return dt.ToShortDateString() + ":" + dt.Hour;
        }

        public CalendarData addEvent(CalendarData c, String title, String content, String startTime, String endTime)
        {

            DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, int.Parse(startTime), 0, 0);

            DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, int.Parse(endTime), 0, 0);

            EventEntry entry = new EventEntry();

            entry.Title.Text = title;
            entry.Content.Content = content;

            When eventTime = new When(start, end);
            entry.Times.Add(eventTime);

            Uri postURI = new Uri("https://www.google.com/calendar/feeds/default/private/full");

            AtomEntry ie = myService.Insert(postURI, entry);

            return new CalendarData(DateTime.Now, this.user, this.pass);
        }

        public String getFeed()
        {
            string str = "";
            for (int i = 0; i < 50; i++) str = str + "</br>";
            foreach (Google.GData.Calendar.EventEntry myEntry in eventFeed.Entries)
            {
                str = str + myEntry.Title.Text + "</br>";
            }
            return str;
        }


        public AtomEntryCollection getEntries()
        {
            return eventFeed.Entries;
        }
    }
}
